package com.example.snakefinal;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;


public class SnakeWayMethodChoice extends AppCompatActivity {
// Used to display the selected skin

    @SuppressLint("UseCompatLoadingForDrawables")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.way_method_choice);

        // Get the ImageView on the left

        // Load the last selected skin resource ID from the SharedPreferences
        SharedPreferences sharedPref = getSharedPreferences("SnakeGamePrefs", Context.MODE_PRIVATE);
        // 1 触摸屏幕
        // 2 陀螺仪感应
        // 3 语音识别
        int snakeWayMethod = sharedPref.getInt("snakeWayMethod", 1);
        Button way1Btn = findViewById(R.id.way1);
        Button way2Btn = findViewById(R.id.way2);
        Button way3Btn = findViewById(R.id.way3);
        // 创建一个GradientDrawable对象
        Drawable borderDrawable = getResources().getDrawable(R.drawable.rounded_button);
        GradientDrawable defaultDrawable = new GradientDrawable();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            defaultDrawable.setPadding(16, 16, 16, 16);
        }
        defaultDrawable.setCornerRadius(25);
        defaultDrawable.setColor(Color.GRAY);
        way1Btn.setBackground(defaultDrawable);
        way2Btn.setBackground(defaultDrawable);
        way3Btn.setBackground(defaultDrawable);
        switch (snakeWayMethod) {
            case 1:
                way1Btn.setBackground(borderDrawable);
                break;
            case 2:
                way2Btn.setBackground(borderDrawable);
                break;
            case 3:
                way3Btn.setBackground(borderDrawable);
                break;
        }

        View.OnClickListener onClickListener = v -> {
            way1Btn.setBackground(defaultDrawable);
            way2Btn.setBackground(defaultDrawable);
            way3Btn.setBackground(defaultDrawable);
            v.setBackground(borderDrawable);
            if (v.equals(way1Btn)) {
                SharedPreferences.Editor editor = sharedPref.edit();
                editor.putInt("snakeWayMethod", 1);
                editor.apply();
            } else if (v.equals(way2Btn)) {
                SharedPreferences.Editor editor = sharedPref.edit();
                editor.putInt("snakeWayMethod", 2);
                editor.apply();
            } else if (v.equals(way3Btn)) {
                SharedPreferences.Editor editor = sharedPref.edit();
                editor.putInt("snakeWayMethod", 3);
                editor.apply();
            }
        };

        way1Btn.setOnClickListener(onClickListener);
        way2Btn.setOnClickListener(onClickListener);
        way3Btn.setOnClickListener(onClickListener);

        // Returns the click event for the button
        Button btnBack = findViewById(R.id.btnBack2);
        btnBack.setBackground(getResources().getDrawable(R.drawable.rounded_button));
        btnBack.setOnClickListener(v -> finish());  // End the current activity and return to the previous page

    }


}
